//
//  EFBChartsVersionSelectController.m
//  muefb
//
//  Created by 蒋 尚秀 on 12-7-16.
//  Copyright (c) 2012年 ADCC. All rights reserved.
//

#import "EFBChartsVersionViewController.h"

@interface EFBChartsVersionViewController ()

@end

@implementation EFBChartsVersionViewController

@synthesize delegate;
@synthesize dataSouceArray = _dataSouceArray;
@synthesize selectedIndex = _selectedIndex;
@synthesize currentIndex = _currentIndex;
@synthesize isExpired = _isExpired;

#define ALERT_EXPIRED 1
#define ALERT_NOT_EFFECTIVE -1

- (id)initWithStyle:(UITableViewStyle)style{
    self = [super initWithStyle:style];
    if (self) {
    }
    
    return self;
}

-(void)dealloc
{ 
    [_dataSouceArray release];
    [_isExpired release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.currentIndex = self.selectedIndex;

    self.tableView.allowsSelection = YES;
    
    self.navigationItem.title = @"航图版本选择";//NSLocalizedString(@"chart-revision-choose",@"航图版本选择");
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelTaped:)] autorelease];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTaped:)] autorelease];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.isExpired = nil;
    self.dataSouceArray = nil;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"缺省版本";//NSLocalizedString(@"chart-revision-default",@"缺省版本");
    }
    else  if(section == 1)
    {
        return @"版本2";//NSLocalizedString(@"chart-revision-another",@"版本2");
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataSouceArray.count == 2) {
        return 1;
    }
    else if(self.dataSouceArray.count == 1)
    {
        if (section == 0) {
            return 1;
        }
        else return 0;
    }
    return 0;
}

- (IBAction)cancelTaped:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UIAlertViewDelegate 方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch(alertView.tag) {
        case ALERT_EXPIRED:
            if (buttonIndex == 1) {
                self.selectedIndex = self.currentIndex;
                [delegate versionChanged:self.selectedIndex];
                [self dismissViewControllerAnimated:YES completion:nil];
            }
            break;
        case ALERT_NOT_EFFECTIVE:
            if (buttonIndex == 1) {
                self.selectedIndex = self.currentIndex;
                [delegate versionChanged:self.selectedIndex];
                [self dismissViewControllerAnimated:YES completion:nil];
            }
            break;
    }
    
}

- (IBAction)doneTaped:(id)sender {
    if (self.currentIndex != self.selectedIndex) {
        if ([[self.isExpired objectAtIndex:self.currentIndex] isEqualToValue:[NSNumber numberWithInt:1]]) {
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"温馨提示:"/*NSLocalizedString(@"alert-title",@"温馨提示:")*/ message:@"该版本已过期，确定要切换吗？" /*NSLocalizedString(@"char-alert-expired", @"该版本已过期，确定要切换吗？")*/ delegate:self cancelButtonTitle: @"取消" /*NSLocalizedString(@"alert-cancel",@"取消")*/ otherButtonTitles:@"确定" /*NSLocalizedString(@"alert-sure",@"确定")*/, nil];
            
            alert.tag = ALERT_EXPIRED;
            [alert show];
            [alert release];
        }
        else if ([[self.isExpired objectAtIndex:self.currentIndex] isEqualToValue:[NSNumber numberWithInt:-1]]) {
                UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"温馨提示:" /*NSLocalizedString(@"alert-title",@"温馨提示:")*/ message:@"该版本未生效，确定要切换吗？" /*NSLocalizedString(@"char-alert-not-effective", @"该版本未生效，确定要切换吗？")*/ delegate:self cancelButtonTitle:@"取消" /* NSLocalizedString(@"alert-cancel",@"取消")*/ otherButtonTitles: @"确定" /*NSLocalizedString(@"alert-sure",@"确定")*/, nil];
                
                alert.tag = ALERT_NOT_EFFECTIVE;
                [alert show];
                [alert release];
            }
        else{
            self.selectedIndex = self.currentIndex;
            [delegate versionChanged:self.selectedIndex];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
    else
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.currentIndex = indexPath.section;
    [tableView reloadData];
}

-(NSString*)dateToString:(NSDate*)date
{
    NSDateFormatter * dateFormat = [[[NSDateFormatter alloc] init] autorelease];
    dateFormat.dateFormat =@"yyyy年MM月dd日";//NSLocalizedString(@"formatter-type-three", @"yyyy年MM月dd日");
    NSString* dateString = [dateFormat stringFromDate:date];
    if(dateString == nil || [dateString isEqualToString:@""])
    {
        dateString = @"长期有效";//NSLocalizedString(@"updatecell-date-null",@"长期有效");
    }
    return dateString;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"DirectoryViewCell";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
	}
    if(self.currentIndex == indexPath.section)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    if ([[self.isExpired objectAtIndex:indexPath.section] isEqualToValue:[NSNumber numberWithInt:1]]) {
        cell.textLabel.textColor = [UIColor redColor];
    }
    else if ([[self.isExpired objectAtIndex:indexPath.section] isEqualToValue:[NSNumber numberWithInt:0]]){
        cell.textLabel.textColor = [UIColor blackColor];
    }
    else
    {
        cell.textLabel.textColor =  [UIColor colorWithRed:198.0/255.0 green:135.0/255.0 blue:40.0/255.0 alpha:1];
    }
    
    NSDateFormatter * dateFormat = [[[NSDateFormatter alloc] init] autorelease];
    dateFormat.dateFormat =@"yyyy-MM-dd";
    NSDate* effiectiveDate = [[self.dataSouceArray objectAtIndex:indexPath.section] objectForKey:@"effective_time"];
    NSDate* endDate = [[[NSDate alloc] initWithTimeInterval:2332800 sinceDate:effiectiveDate] autorelease];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@(%@--%@)",[[self.dataSouceArray objectAtIndex:indexPath.section]  objectForKey:@"revision_nver"],[dateFormat stringFromDate:effiectiveDate],[dateFormat stringFromDate:endDate]];
    
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@：%@  %@：%@",@"生效日期"/*NSLocalizedString(@"updatecell-effective-date", @"生效日期")*/, [self dateToString: [[self.dataSouceArray objectAtIndex:indexPath.section] objectForKey:@"effective_time"]],@"失效日期"/*NSLocalizedString(@"updatecell-expire-date", @"失效日期")*/,[self dateToString:[[self.dataSouceArray objectAtIndex:indexPath.section] objectForKey:@"expire_time"]]];
    
    
    
	return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
@end
